<?php

namespace app\admin\controller\mall\goods;

use app\admin\controller\mall\Base;

/**
 * 商品品牌
 *
 * @icon fa fa-circle-o
 */
class Brands extends Base
{
    
    /**
     * Brands模型对象
     * @var \app\common\model\mall\goods\Brand
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\mall\goods\Brand;

    }

    public function selectpage()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'htmlspecialchars']);

        //搜索关键词,客户端输入以空格分开,这里接收为数组
        $word = (array) $this->request->request("q_word/a");
        //当前页
        $page = $this->request->request("pageNumber");
        //分页大小
        $pagesize = $this->request->request("pageSize");
        //搜索条件
        $andor = $this->request->request("andOr", "and", "strtoupper");
        //排序方式
        $orderby = (array) $this->request->request("orderBy/a");
        //显示的字段
        $field = $this->request->request("showField",'name');
        //主键
        $primarykey = $this->request->request("keyField",'id');
        //主键值
        $primaryvalue = $this->request->request("keyValue");
        //搜索字段
        $searchfield = (array) $this->request->request("searchField/a");
        //自定义搜索条件
        $custom = (array) $this->request->request("custom/a");
        $order = [];
        foreach ($orderby as $k => $v)
        {
            $order[$v[0]] = $v[1];
        }
        $field = $field ? $field : 'name';

        //如果有primaryvalue,说明当前是初始化传值
        if ($primaryvalue !== null){
        	$where = [$primarykey => ['in', $primaryvalue]];
        }
        else
        {
            $where = function ($query) use ($word, $andor, $field, $searchfield) {
                $logic = $andor == 'AND' ? '&' : '|';
                $searchfield = is_array($searchfield) ? implode($logic, $searchfield) : $searchfield;
                foreach ($word as $k => $v) {
                    $query->where(str_replace(',', $logic, $searchfield), "like", "%{$v}%");
                }
                if ($custom && is_array($custom)) {
                	foreach ($custom as $k => $v) {
                		if (is_array($v) && 2 == count($v)) {
                			$query->where($k, trim($v[0]), $v[1]);
                		} else {
                			$query->where($k, '=', $v);
                		}
                	}
                }
            };
        }

        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
        	$this->model->where($this->dataLimitField, 'in', $adminIds);
        }
        
        $list = [];
        $cat_ids=db("mall_goods_catbrand")->where($custom)->column("brandId");
        $total = $this->model
            ->where("id","in",$cat_ids)
            ->where($where)
            ->count();
        if ($total > 0)
        {
            $datalist = $this->model
                ->where("id","in",$cat_ids)
                ->where($where)
                ->order($order)
                ->page($page, $pagesize)
                ->field($this->selectpageFields)
                ->select();
            foreach ($datalist as $index => $item) {
                unset($item['password'], $item['salt']);
                $list[] = [
                    $primarykey => isset($item[$primarykey]) ? $item[$primarykey] : '',
                    $field      => isset($item[$field]) ? $item[$field] : ''
                ];
            }
        }
        //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
        return json(['list' => $list, 'total' => $total]);
    }

}
